package com.manager.car.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.manager.car.po.AutoPartsInfo;
import com.manager.car.service.APIservice;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class APIinfoAction extends ActionSupport{

	private APIservice apiservice;
	private Integer id;
	private Integer ye=1;//当前页
	private Integer bian=0;//翻页的变量
	private String pro;//分类的属性
	
	//添加修改的变量
	private String autoName;
	private String autoprice;
	private String autonum;
	private String autotype;
	private String autofa;
	private String autoxiu;
	private String autoPro;
	private File autoImg;//也没文本域表单name
	private String autoImgFileName;//文件名称，表单name+FileName
	private String autoImgContentType;//文件类型，表单name+ContentTYpe
	private String autoJie;
	private String autoRemark;
	
	
	
	
	//查询首页
	public String selectBegin() {
		List<AutoPartsInfo> list=apiservice.queryAllBegin();
		ActionContext.getContext().put("list", list);
		return "selectBegin";
	}
	//前台查询所有产品分页
	public String selectPage() {
		int row=(int)apiservice.queryAllRow(12);
		ye=ye+bian;bian=0;
		if(ye<1) {
			ye=1;
		}if(ye>row) {
			ye=row;
		}
		List<AutoPartsInfo> list=apiservice.queryAllPage(ye,12);
		ActionContext.getContext().put("pro", "");
		ActionContext.getContext().put("list", list);
		return "selectPage";
	}
	//后台查询所有产品分页
		public String selectHtPage() {
			int row=(int)apiservice.queryAllRow(6);
			ye=ye+bian;bian=0;
			if(ye<1) {
				ye=1;
			}if(ye>row) {
				ye=row;
			}
			List<AutoPartsInfo> list=apiservice.queryAllPage(ye,6);
			ActionContext.getContext().put("pro", "");
			ActionContext.getContext().put("list", list);
			return "selectHtPage";
		}
	//分类查询所有产品
		public String selectPro() {
			int row=(int)apiservice.queryProRow(pro);
			ye=ye+bian;bian=0;
			if(ye<1) {
				ye=1;
			}if(ye>row) {
				ye=row;
			}
			List<AutoPartsInfo> list=apiservice.queryProPage(ye,pro);
			ActionContext.getContext().put("pro", pro);
			ActionContext.getContext().put("list", list);
			return "selectPro";
		}
	//添加
	public String add() throws IOException {
		AutoPartsInfo api=new AutoPartsInfo();
		api.setAutoName(autoName);
		api.setAutoprice(autoprice);
		api.setAutonum(autonum);
		api.setAutotype(autotype);
		api.setAutofa(autofa);
		api.setAutoxiu(autoxiu);
		api.setAutoPro(autoPro);
		api.setAutoImg("img/"+autoImgFileName);
		api.setAutoJie(autoJie);
		api.setAutoRemark(autoRemark);
		//文件上传路径
		String path=ServletActionContext.getServletContext().getRealPath("/img/");
		path+=autoImgFileName;
		//文件的输入输出流
		FileOutputStream fos=new FileOutputStream(path);
		FileInputStream fis=new FileInputStream(autoImg);
		byte[] b=new byte[1024];
		int len =0;
		while((len=fis.read(b))!=-1) {
			fos.write(b,0,len);
		}
		//关闭
		fis.close();
		fos.close();
		apiservice.add(api);
		return "add";
	}
	
	public String upda() throws IOException {
		AutoPartsInfo api=new AutoPartsInfo();
		api.setAutoId(id);
		api.setAutoName(autoName);
		api.setAutoprice(autoprice);
		api.setAutonum(autonum);
		api.setAutotype(autotype);
		api.setAutofa(autofa);
		api.setAutoxiu(autoxiu);
		api.setAutoPro(autoPro);
		api.setAutoImg("img/"+autoImgFileName);
		api.setAutoJie(autoJie);
		api.setAutoRemark(autoRemark);
		//文件上传路径
		String path=ServletActionContext.getServletContext().getRealPath("/img/");
		path+=autoImgFileName;
		//文件的输入输出流
		FileOutputStream fos=new FileOutputStream(path);
		FileInputStream fis=new FileInputStream(autoImg);
		byte[] b=new byte[1024];
		int len =0;
		while((len=fis.read(b))!=-1) {
			fos.write(b,0,len);
		}
		//关闭
		fis.close();
		fos.close();
		apiservice.update(api);
		return "update";
	}
	
	public String del() {
		apiservice.delete(id);
		return "delete";
	}
	//前台根据id查询
	public String get() {
		AutoPartsInfo api = apiservice.get(id);
		ActionContext.getContext().put("api", api);
		return "selectOnly";
	}
	//后台根据id查询
	public String getHt() {
		AutoPartsInfo api = apiservice.get(id);
		ActionContext.getContext().put("api", api);
		return "selectOnlyHt";
	}

	public APIservice getApiservice() {
		return apiservice;
	}
	public void setApiservice(APIservice apiservice) {
		this.apiservice = apiservice;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public Integer getYe() {
		return ye;
	}
	public void setYe(Integer ye) {
		this.ye = ye;
	}
	public Integer getBian() {
		return bian;
	}
	public void setBian(Integer bian) {
		this.bian = bian;
	}
	public String getPro() {
		return pro;
	}
	public void setPro(String pro) {
		this.pro = pro;
	}
	public String getAutoName() {
		return autoName;
	}
	public void setAutoName(String autoName) {
		this.autoName = autoName;
	}
	public String getAutoprice() {
		return autoprice;
	}
	public void setAutoprice(String autoprice) {
		this.autoprice = autoprice;
	}
	public String getAutonum() {
		return autonum;
	}
	public void setAutonum(String autonum) {
		this.autonum = autonum;
	}
	public String getAutotype() {
		return autotype;
	}
	public void setAutotype(String autotype) {
		this.autotype = autotype;
	}
	public String getAutofa() {
		return autofa;
	}
	public void setAutofa(String autofa) {
		this.autofa = autofa;
	}
	public String getAutoxiu() {
		return autoxiu;
	}
	public void setAutoxiu(String autoxiu) {
		this.autoxiu = autoxiu;
	}
	public String getAutoPro() {
		return autoPro;
	}
	public void setAutoPro(String autoPro) {
		this.autoPro = autoPro;
	}
	
	public File getAutoImg() {
		return autoImg;
	}
	public void setAutoImg(File autoImg) {
		this.autoImg = autoImg;
	}
	public String getAutoImgFileName() {
		return autoImgFileName;
	}
	public void setAutoImgFileName(String autoImgFileName) {
		this.autoImgFileName = autoImgFileName;
	}
	public String getAutoImgContentType() {
		return autoImgContentType;
	}
	public void setAutoImgContentType(String autoImgContentType) {
		this.autoImgContentType = autoImgContentType;
	}
	public String getAutoJie() {
		return autoJie;
	}
	public void setAutoJie(String autoJie) {
		this.autoJie = autoJie;
	}
	public String getAutoRemark() {
		return autoRemark;
	}
	public void setAutoRemark(String autoRemark) {
		this.autoRemark = autoRemark;
	}
	
	
}
